package com.bdyc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bdyc.controller.custom.MapVo;
import com.bdyc.controller.custom.MyPo;
import com.bdyc.controller.custom.UserInfoPo;
import com.bdyc.po.UserInfo;
import com.bdyc.service.spring.UserInfoService;
import com.bdyc.util.AppConstant;

@Controller
@RequestMapping("/userInfo")
public class UserController {
	@Autowired(required=true)
	@Qualifier("userInfoService")
	private UserInfoService userServiceImpl;
	//添加userInfo方法
		@RequestMapping(value="/addUserInfo.action",method=RequestMethod.POST)
		@ResponseBody//返回json数据注解
		public Map<String,Object> addManagerInfo(UserInfo userInfo){
			Map<String,Object> map = new HashMap<String,Object>();
			try{
					//插入userInfo
					userServiceImpl.addUserInfo(userInfo);
					map.put(AppConstant.ISSUCC, AppConstant.SUCCESS);
					map.put(AppConstant.MSG, AppConstant.ADDSUCCESS);
					return map;
			}catch(Exception e){
				e.printStackTrace();
				map.put(AppConstant.ISSUCC,AppConstant.FALL);
				map.put(AppConstant.MSG,AppConstant.ADDFALL);
				return map;
			}
		}
		//判断客户是否唯一方法入口,注册时可判断是否用户名重复
		@RequestMapping(value="/onlyName.action",method=RequestMethod.POST)
		@ResponseBody//返回json数据注解
		public Map<String,Object> onlyName(String name){
			Map<String,Object> map = new HashMap<String,Object>();
			try{
				//查询userInfo，managerInfo表 判断用户名是否存在
				Integer count = userServiceImpl.selectName(name);
				if(count >0){
					map.put(AppConstant.ISSUCC,AppConstant.FALL);
					map.put(AppConstant.MSG,"用户名不可用!");
				}else{
					map.put(AppConstant.ISSUCC, AppConstant.SUCCESS);
					map.put(AppConstant.MSG,"用户名可用!");
				}
				return map;
			}catch(Exception e){
				e.printStackTrace();
				map.put(AppConstant.ISSUCC,AppConstant.FALL);
				map.put(AppConstant.MSG, "服务器异常，请稍后重试!");
				return map;
			}
		}
		//登录方法
		@RequestMapping(value="/login.action",method=RequestMethod.GET)
		@ResponseBody//返回json数据注解
		public Map<String,Object> login(String name,String pass,HttpSession session){
			Map<String,Object> map = new HashMap<String,Object>();
			try{
				UserInfoPo userInfoPo = userServiceImpl.login(name,pass);
				if(userInfoPo != null){
					session.setAttribute("info", userInfoPo);
					//允许客户登录
					map.put(AppConstant.MSG,"登录成功!");
					map.put(AppConstant.ISSUCC,AppConstant.SUCCESS);
				}else{
					map.put(AppConstant.ISSUCC,AppConstant.FALL);
					map.put(AppConstant.MSG, "用户名或密码错误!请重新登录!");
				}
				return map;
			}catch(Exception e){
				e.printStackTrace();
				map.put(AppConstant.ISSUCC,AppConstant.FALL);
				map.put(AppConstant.MSG, "服务器异常，请稍后重试!");
				return map;
			}
		}
		//获取userList
		@RequestMapping(value="/findUserInfoList.action",method=RequestMethod.GET)
		@ResponseBody//返回json数据注解
		public Map<String,Object> findUserInfoList(MyPo po){
			Map<String,Object> map = new HashMap<String,Object>();
			//封装分页查询map
			//封装分页查询总记录数map
			Map<String,Object> sendMap = MapVo.getSendMap(po);
			Integer rows = po.getRows();
			try{
				List<UserInfoPo> infoList = userServiceImpl.findUserInfoList(sendMap);
				Integer count = userServiceImpl.findUserInfoListCount(sendMap);
				Integer index = count/rows;
				Integer sum = count%rows==0?index:index +1;
				map.put("sum", sum);
				map.put("count", count);
				map.put("info", infoList);
				map.put(AppConstant.ISSUCC,AppConstant.SUCCESS);
				map.put(AppConstant.MSG,AppConstant.FINDSUCCESS);
				return map;
			}catch(Exception e){
				e.printStackTrace();
				map.put(AppConstant.ISSUCC,AppConstant.FALL);
				map.put(AppConstant.MSG,AppConstant.FINDFALL );
				return map;
			}
		}
}
